﻿using Common.Communal;
using ERP.Domain.Scheduling;
using Scheduling.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scheduling.Services.TeamMembers
{
	public class TeamMemberServices : ITeamMemberServices
	{
		public TeamMemberServices(ITeamMemberRepository teamMemberRepository)
		{
			TeamMemberRepository = teamMemberRepository;
		}

		public ITeamMemberRepository TeamMemberRepository { get; }

		public async Task<ApiResult<int>> AddTeamMember(TeamMember team)
		{
			ApiResult<int> result = new ApiResult<int>();
			try
			{
				if (team == null)
				{
					result.Code = EnumCode.Fail;
					result.Message = "成员信息不能为空";
					return result;
				}

				if (team.TeamId <= 0)
				{
					result.Code = EnumCode.Fail;
					result.Message = "TeamId 无效";
					return result;
				}

				if (team.EmployeeId <= 0)
				{
					result.Code = EnumCode.Fail;
					result.Message = "EmployeeId 无效";
					return result;
				}

				var affected = await TeamMemberRepository.AddAsync(team);
				result.Code = EnumCode.Ok;
				result.Message = "成员添加成功";
				result.Data = affected;
			}
			catch (Exception ex)
			{
				result.Code = EnumCode.Fail;
				result.Message = $"添加成员失败：{ex.Message}";
			}

			return await Task.FromResult(result);
		}

        public async Task<ApiResult<int>> UpdTeamMember(TeamMember team)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                if (team == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "成员信息不能为空";
                    return result;
                }

                if (team.MemberId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "MemberId 无效";
                    return result;
                }

                if (team.TeamId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "TeamId 无效";
                    return result;
                }

                if (team.EmployeeId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "EmployeeId 无效";
                    return result;
                }

                var affected = await TeamMemberRepository.Update(team);
                result.Code = EnumCode.Ok;
                result.Message = "成员修改成功";
                result.Data = affected;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"修改成员失败：{ex.Message}";
            }

            return await Task.FromResult(result);
        }

        public async Task<ApiResult<int>> DeleteTeamMember(int memberId)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                if (memberId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "MemberId 无效";
                    return result;
                }

                // 先查询是否存在该成员
                var existingMember = await TeamMemberRepository.GetModelAsync(memberId);
                if (existingMember == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "成员不存在";
                    return result;
                }

                // 软删除：设置IsDelete为true
                existingMember.IsDelete = true;
                var affected = await TeamMemberRepository.Update(existingMember);
                
                result.Code = EnumCode.Ok;
                result.Message = "成员删除成功";
                result.Data = affected;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"删除成员失败：{ex.Message}";
            }

            return await Task.FromResult(result);
        }

        public async Task<ApiResult<List<TeamMember>>> GetTeamMembersByTeamId(int teamId)
        {
            ApiResult<List<TeamMember>> result = new ApiResult<List<TeamMember>>();
            try
            {
                if (teamId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "TeamId 无效";
                    return result;
                }

                var teamMembers = await TeamMemberRepository.GetAsync(x => x.TeamId == teamId && x.IsDelete == false);
                result.Code = EnumCode.Ok;
                result.Message = "查询班组成员成功";
                result.Data = teamMembers;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"查询班组成员失败：{ex.Message}";
            }

            return await Task.FromResult(result);
        }
    }
}
